using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SpaceEventManager
{
    public partial class SpaceEditorGUI : Form
    {
        SpaceManager spaceManager;
        Dictionary<string, Space> dictSpaces;
        Boolean drag;
        Control dragControl;
        MainGUI caller;
        public SpaceEditorGUI(SpaceManager sMgr, MainGUI iMain)
        {
            drag = false;
            spaceManager = sMgr;
            caller = iMain;
            InitializeComponent();
            dictSpaces = new Dictionary<string, Space>();
            foreach (List<Space> list in spaceManager.SpaceList.AllSpaces)
            {
                foreach (Space s in list)
                {
                    lstBxAllSpaces.Items.Add(s.Name);
                    dictSpaces.Add(s.Name, s);
                }
            }
        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length > 0 && txtDimX.Text.Length > 0 && txtDimY.Text.Length > 0)
            {
                Label temp = new Label();
                temp.AutoSize = false;
                temp.Text = txtName.Text;
                temp.BorderStyle = BorderStyle.FixedSingle;
                temp.Location = new Point(sldXPos.Value, sldYPos.Maximum - sldYPos.Value);

                temp.Size = new Size(Convert.ToInt16(txtDimX.Text), Convert.ToInt16(txtDimY.Text));

                pnlDraw.Controls.Clear();
                pnlDraw.Controls.Add(temp);
                pnlDraw.Invalidate();

                btnCreateSpace.Enabled = true;

            }
            else
            {
                MessageBox.Show("Please fill in the Name and Dimension fields.", "Error");
            }
        }

        private void btnCreateSpace_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length > 0 && txtDimX.Text.Length > 0 && txtDimY.Text.Length > 0)
            {
                if (spaceManager.SearchSpace(txtName.Text) < 0)
                {
                    spaceManager.AddSpace(txtName.Text, txtNotes.Text, new Point(Convert.ToInt16(txtDimX.Text), Convert.ToInt16(txtDimY.Text)), new Dictionary<Space, Point>());
                    txtName.Text = "";
                    txtNotes.Text = "";
                    txtDimX.Text = "";
                    txtDimY.Text = "";
                    UpdateList();
                }
                else
                {
                    MessageBox.Show("You have already entered an existing space with this name, please use a different name.", "Error");
                }
            }
            else
            {
                MessageBox.Show("Please fill in the Name and Dimension fields.", "Error");
            }

            caller.UpdateSpaceList();
        }

        private void btnRemoveSpace_Click(object sender, EventArgs e)
        {
            if (lstBxAllSpaces.SelectedItem != null)
            {
                spaceManager.RemoveSpace(lstBxAllSpaces.SelectedItem.ToString());
                txtName.Text = "";
                txtNotes.Text = "";
                txtDimX.Text = "";
                txtDimY.Text = "";
                UpdateList();
            }
            else
            {
                MessageBox.Show("You need to choose an existing space to remove.", "Error");
            }

            caller.UpdateSpaceList();
        }

        private void btnSearchSpace_Click(object sender, EventArgs e)
        {
            txtDimX.Text = "";
            txtDimY.Text = "";
            if (spaceManager.SearchSpace(txtName.Text) >= 0)
            {
                MessageBox.Show(txtName.Text + " was found in the Spacelist.", "Success");
            }
            else
            {
                MessageBox.Show(txtName.Text + " was not found in the Spacelist.", "Failure");
            }
        }

        private void btnAddAsInnerSpace_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Length > 0 && txtDimX.Text.Length > 0 && txtDimY.Text.Length > 0)
            {
                if ((spaceManager.SearchSpace(txtName.Text) < 0) && (lstBxAllSpaces.SelectedItem != null))
                {
                    spaceManager.AddInnerSpace(lstBxAllSpaces.SelectedItem.ToString(), txtName.Text, txtNotes.Text, new Point(Convert.ToInt16(txtDimX.Text), Convert.ToInt16(txtDimY.Text)), new Dictionary<Space, Point>());
                    txtName.Text = "";
                    txtNotes.Text = "";
                    txtDimX.Text = "";
                    txtDimY.Text = "";
                    UpdateList();
                }
                else if (lstBxAllSpaces.SelectedItem != null)
                {
                    MessageBox.Show("You have already entered an existing space with this name, please use a different name.", "Error");
                }
                else
                {
                    MessageBox.Show("You need to choose an existing space to add an inner space to.", "Error");
                }
            }
            else
            {
                MessageBox.Show("Please fill in the Name and Dimension fields.", "Error");
            }

            caller.UpdateSpaceList();
        }

        /* Prince added this method to redraw this list after whatever actions */
        private void UpdateList()
        {
            lstBxAllSpaces.Items.Clear();
            dictSpaces.Clear();
            foreach (List<Space> list in spaceManager.SpaceList.AllSpaces)
            {
                foreach (Space s in list)
                {
                    lstBxAllSpaces.Items.Add(s.Name);
                    dictSpaces.Add(s.Name, s);
                }
            }
        }

        private void UpdatePnlDraw()
        {
            if (txtName.Text.Length > 0 && txtDimX.Text.Length > 0 && txtDimY.Text.Length > 0)
            {
                double ratio;
                Label temp = new Label();
                temp.AutoSize = false;
                temp.Text = txtName.Text;
                temp.BorderStyle = BorderStyle.FixedSingle;
                temp.Location = new Point(25, 25);
                //temp.Location = new Point(sldXPos.Value, sldYPos.Maximum - sldYPos.Value);
                int width = Convert.ToInt16(txtDimX.Text);
                int height = Convert.ToInt16(txtDimY.Text);
                if (width > height)
                {
                    temp.Width = pnlDraw.Width - 50;
                    ratio = Convert.ToDouble(temp.Width) / Convert.ToDouble(width);
                    temp.Height = Convert.ToInt16(height * ratio);
                }
                else
                {
                    temp.Height = pnlDraw.Height - 50;
                    ratio = Convert.ToDouble(temp.Height) / Convert.ToDouble(height);
                    temp.Width = Convert.ToInt16(width * ratio);
                }

                //temp.Size = new Size(Convert.ToInt16(txtDimX.Text), Convert.ToInt16(txtDimY.Text));

                pnlDraw.Controls.Clear();

                if (dictSpaces.ContainsKey(temp.Text))
                {
                    Space refSpace = dictSpaces[temp.Text];
                    foreach (Space s in refSpace.InnerSpaces.Keys)
                    {
                        Label innerSpace = new Label();
                        innerSpace.BorderStyle = BorderStyle.FixedSingle;
                        innerSpace.AutoSize = false;
                        innerSpace.Text = s.Name;
                        //MessageBox.Show("Inner Space 1 = " + innerSpace.Text + "; ratio is: " + ratio);
                        innerSpace.Width = Convert.ToInt16(s.Dimensions.X * ratio);
                        innerSpace.Height = Convert.ToInt16(s.Dimensions.Y * ratio);
                        //MessageBox.Show(innerSpace.Text + "'s size is: " + innerSpace.Width + ", " + innerSpace.Height);
                        //innerSpace.Location = new Point(Convert.ToInt16(25 + (iSpace.InnerSpaces[s].X * ratio)), Convert.ToInt16(25 + (iSpace.InnerSpaces[s].Y * ratio)));
                        innerSpace.Left = Convert.ToInt16(25 + (refSpace.InnerSpaces[s].X * ratio));
                        innerSpace.Top = Convert.ToInt16(25 + (refSpace.InnerSpaces[s].Y * ratio));
                        innerSpace.MouseDown += new MouseEventHandler(innerSpace_MouseDown);
                        innerSpace.MouseMove += new MouseEventHandler(innerSpace_MouseMove);
                        innerSpace.MouseUp += new MouseEventHandler(innerSpace_MouseUp);
                        innerSpace.DoubleClick += new EventHandler(innerSpace_DoubleClick);
                        pnlDraw.Controls.Add(innerSpace);
                    }
                }

                pnlDraw.Controls.Add(temp);
                pnlDraw.Invalidate();

                btnCreateSpace.Enabled = true;
            }
        }

        void innerSpace_MouseUp(object sender, MouseEventArgs e)
        {
            drag = false;
        }

        void innerSpace_MouseMove(object sender, MouseEventArgs e)
        {
            if (drag == true && sender.Equals(dragControl))
            {
                //MessageBox.Show("Mouse Location: " + e.Location.ToString() + ", Control location: " + dragControl.Location.ToString());
                //dragControl.Location = e.Location - pnlDraw.Location;
                //MessageBox.Show("Mouse Location: " + e.Location.ToString() + ", Control location: " + dragControl.Location.ToString());
                dragControl.Top = e.Y + dragControl.Top;
                dragControl.Left = e.X + dragControl.Left;
                //pnlDraw.Invalidate();
            }
        }

        void innerSpace_MouseDown(object sender, MouseEventArgs e)
        {
            foreach (Control c in pnlDraw.Controls)
            {
                if (sender.Equals(c))
                {
                    dragControl = c;
                    drag = true;
                    //c.Location = e.Location;
                }
            }
        }

        void innerSpace_DoubleClick(object sender, EventArgs e)
        {
            string temp = sender.ToString();
            int index = temp.LastIndexOf("Text: ") + 6;
            Space iSpace = dictSpaces[temp.Substring(index)];
            UpdatePnlDraw();
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            UpdatePnlDraw();
        }

        private void txtDimX_TextChanged(object sender, EventArgs e)
        {
            UpdatePnlDraw();
        }

        private void txtDimY_TextChanged(object sender, EventArgs e)
        {
            UpdatePnlDraw();
        }

        private void sldXPos_Scroll(object sender, EventArgs e)
        {
            UpdatePnlDraw();
        }

        private void sldYPos_Scroll(object sender, EventArgs e)
        {
            UpdatePnlDraw();
        }

        private void lstBxAllSpaces_SelectedIndexChanged(object sender, EventArgs e)
        {
            Space temp = dictSpaces[lstBxAllSpaces.SelectedItem.ToString()];
            txtName.Text = temp.Name;
            txtNotes.Text = temp.Notes;
            txtDimX.Text = temp.Dimensions.X.ToString();
            txtDimY.Text = temp.Dimensions.Y.ToString();
            lstBxInSpaces.Items.Clear();
            foreach (Space s in temp.InnerSpaces.Keys)
            {
                lstBxInSpaces.Items.Add(s.Name);
            }
        }

        private void lstBxInSpaces_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstBxAllSpaces.SelectedIndex = lstBxAllSpaces.FindStringExact(lstBxInSpaces.SelectedItem.ToString());
        }
    }
}